﻿using System.Text;
using OSS.NBEncode.Entities;

namespace OSS.NBEncode.Extensions
{
	public static class BListExtensions
	{
		public static IBObject FindSection(this BList list, string sectionName)
		{
			IBObject result = null;

			for (int i = 0; i < list.Value.Length; i++)
			{
				IBObject bObject = list.Value[i];

				if (bObject is BByteString)
				{
					string key = ((BByteString)bObject).ConvertToText(Encoding.ASCII);
					if (key == sectionName)
					{
						if (i < list.Value.Length - 1)
							result = list.Value[i + 1];
					}
				}

				if (bObject is BDictionary)
					result = ((BDictionary)bObject).FindSection(sectionName);

				if (bObject is BList)
					result = FindSection((BList)bObject, sectionName);

				if (result != null)
					return result;

			}

			foreach (IBObject bObject in list.Value)
			{
				
				
			}

			return null;
		}
	}
}
